package com.smasher.apknaming

import com.android.build.gradle.AppExtension
import org.gradle.api.Plugin
import org.gradle.api.Project

import java.text.SimpleDateFormat

class NamingAndMovePlugin implements Plugin<Project> {

    def timeFormat = new SimpleDateFormat("yyMMdd.HHmm", Locale.getDefault())

    @Override
    void apply(Project project) {
        // 创建扩展
        project.getExtensions().create("apkNaming", ApkNamingExtension.class)

        def android = project.getExtensions().getByType(AppExtension.class)

        // 配置变体
        android.applicationVariants.configureEach { variant ->
            def extension = project.getExtensions().getByType(ApkNamingExtension.class)
            def packageNameDefine = extension.prefix ?: "APK"
            def outputDirectoryName = extension.outputDirectory ?: "apks"
            def copyBuildTypes = extension.copyBuildTypes ?: ["release"] as Set
            def createTime = timeFormat.format(new Date())
            def versionName = variant.getMergedFlavor().getVersionName()
            def versionCode = variant.getMergedFlavor().getVersionCode()
            def buildTypeName = variant.getBuildType().getName()

            // 设置输出文件名
            variant.outputs.configureEach { output ->
                String fileName = String.format("%s%s.%d_%s_%s.apk",
                        packageNameDefine,
                        versionName,
                        versionCode,
                        createTime,
                        buildTypeName)
                output.setOutputFileName(fileName);
            }

            // 对指定的构建类型处理APK复制
            if (copyBuildTypes.contains(buildTypeName)) {
                variant.getAssembleProvider().get().doLast(task -> {
                    // 创建目标目录
                    File outputFileDir = new File(project.getRootDir(), outputDirectoryName);
                    if (!outputFileDir.exists()) {
                        outputFileDir.mkdirs();
                    }
                    def applicationProvider = variant.getPackageApplicationProvider()
                    def apkOutputDirProvider = applicationProvider.get().getOutputDirectory()
                    File apkOutputFileDir = apkOutputDirProvider.get().getAsFile();

                    // 获取APK文件名
                    String targetOutputFileName = variant.getOutputs().iterator().next().getOutputFileName();

                    // APK文件
                    File apkFile = new File(apkOutputFileDir, targetOutputFileName);
                    if (apkFile.exists()) {
                        // 执行复制操作
                        project.copy(copySpec -> {
                            copySpec.from(apkFile)
                            copySpec.into(outputFileDir)
                        })

                        System.out.println("APK " + targetOutputFileName + " 已复制到: " + outputFileDir);
                    } else {
                        System.out.println("APK 文件不存在: " + apkFile);
                    }
                })
            }
        }
    }

}